<?php
include_once('prepend.inc.php');

class PeopleWidget extends QDialogBox
{
	public $dtrPeople;
	protected $pnlPeople;
	public $txtSearchPeople;
	public $btnSearchPeople;
	protected $blnMatteClickable = true;
	protected $strTemplate = 'PeopleWidget.tpl.php';
    protected $fltInternalValue;
    protected $strCurrentOperation;
    protected $blnNextClears;
    	
	public function __construct($strCloseCallback=null, $objParentObject, $strControlId = null)
	{
		parent::__construct($objParentObject, $strControlId);
		
		$this->txtSearchPeople_Create();
		$this->btnSearchPeople_Create();
		$this->dtrPeople_Create();
		$this->LoadPeople();
	}
	

	public function txtSearchPeople_Create()
	{
		$this->txtSearchPeople = new QTextBox($this);
		$this->txtSearchPeople->Name = 'Search Contacts';
	}
	public function btnSearchPeople_Create()
	{
		$this->btnSearchPeople = new QButton($this);
		$this->btnSearchPeople->Text = 'Search';
		$this->btnSearchPeople->AddAction(new QClickEvent(),new QServerControlAction($this,'PeopleSearch_Click'));
	}
	public function LoadPeople()
	{
		$this->dtrPeople_Bind();
	}
	public function dtrPeople_Create()
	{
		$this->dtrPeople = new QDataRepeater($this);
		$this->dtrPeople->Paginator = new QPaginator($this);
		$this->dtrPeople->CssClass = 'contactHover';
		$this->dtrPeople->ItemsPerPage = 7;
		$this->dtrPeople->UseAjax = false;
		$this->dtrPeople->Template = 'pnlPeople.tpl.php';
		$this->dtrPeople->SetDataBinder('dtrPeople_Bind',$this);
	}
	
	function dtrPeople_Bind()
	{
		$this->GetPeopleData();
		
	}
	
	public function GetPeopleData()
	{
		$objClauses = array();
		array_push($objClauses, QQ::OrderBy(QQN::Peopledetails()->FullName));
		if ($objClause = $this->dtrPeople->LimitClause)
		{
			array_push($objClauses, $this->dtrPeople->LimitClause);
		}
		
		
		if($this->txtSearchPeople->Text <> '')
		{
			$condition = QQ::AndCondition(QQ::OrCondition(	
							QQ::Like(QQN::Peopledetails()->FullName,'%'. $this->txtSearchPeople->Text . '%'),
							QQ::Like(QQN::Peopledetails()->Email,'%'. $this->txtSearchPeople->Text . '%'),
							QQ::Like(QQN::Peopledetails()->Phone,'%'. $this->txtSearchPeople->Text . '%')
										),QQ::Equal(QQN::Peopledetails()->Owner,$_SESSION['User']));
			$result = Peopledetails::QueryArray($condition,$objClause);
			$this->dtrPeople->DataSource = $result;
			$this->dtrPeople->TotalItemCount = Peopledetails::QueryCount($condition);
		}
		else
		{
			$condition = QQ::Equal(QQN::Peopledetails()->Owner,$_SESSION['User']);
			$result = Peopledetails::QueryArray($condition,$objClause);
			$this->dtrPeople->DataSource = $result;
			$this->dtrPeople->TotalItemCount = Peopledetails::QueryCount(QQ::All());
		}

	}
	
	public function PeopleSearch_Click()
	{
		$this->dtrPeople_Bind();
	}

	function ShowDialogBox()
	{
		parent::ShowDialogBox();
	}
}


?>